//
//  String+UserController+Extension.swift
//  CMDB
//
//  Created by 王磊 on 17/4/5.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit
// 字符串长度
extension String {
    
    internal var cmdb_length: Int {
        
        return lengthOfBytesUsingEncoding(NSUTF8StringEncoding)
    }
}

extension String {
    // 设备名
    internal static func correspondVersion() -> String? {
        let name = UnsafeMutablePointer<utsname>.alloc(1)
        uname(name)
        let machine = withUnsafePointer(&name.memory.machine, { (ptr) -> String? in
            
            let int8Ptr = unsafeBitCast(ptr, UnsafePointer<CChar>.self)
            
            return String.fromCString(int8Ptr)
        })
        
        name.dealloc(1)
        
        if let deviceString = machine {
            switch deviceString {
            //iPhone
            case "iPhone1,1":                return "iPhone 1G"
            case "iPhone1,2":                return "iPhone 3G"
            case "iPhone2,1":                return "iPhone 3GS"
            case "iPhone3,1", "iPhone3,2":   return "iPhone 4"
            case "iPhone4,1":                return "iPhone 4S"
            case "iPhone5,1", "iPhone5,2":   return "iPhone 5"
            case "iPhone5,3", "iPhone5,4":   return "iPhone 5C"
            case "iPhone6,1", "iPhone6,2":   return "iPhone 5S"
            case "iPhone7,1":                return "iPhone 6 Plus"
            case "iPhone7,2":                return "iPhone 6"
            case "iPhone8,1":                return "iPhone 6s"
            case "iPhone8,2":                return "iPhone 6s Plus"
            default:
                return deviceString
            }
        } else {
            return "Unknown"
        }
    }
}
// 合法手机号
extension String {
    
    internal func validPhoneNumber() -> Bool {
        
        if cmdb_length == 11 {
            
            return self.hasPrefix("1")
        }
        return false
    }
    
    internal func isEmptyPhoneNumber() -> Bool {
        
        if cmdb_length == 0 {
            return true
        }
        return false
    }
}
extension String {
    
    func validEmail() -> Bool {
        //      "/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/"
        //        "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
        let matcher = TSRegex("[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}")
        
        return matcher.match(self as String)
    }
}
// 有效 邮件
struct TSRegex {
    
    let regex: NSRegularExpression?
    
    init(_ pattern: String) {
        try! regex = NSRegularExpression(pattern: pattern,
                                         options: NSRegularExpressionOptions.CaseInsensitive
            
        )
    }
    
    func match(input: String) -> Bool {
        if let matches = regex?.matchesInString(input,
                                                options: NSMatchingOptions.ReportProgress,
                                                range: NSMakeRange(0, input.characters.count)) {
            return matches.count > 0
        } else {
            return false
        }
    }
}

